package json5

import (
	"bufio"
	"bytes"
	"strings"
	"testing"
)

func Test_Json(t *testing.T) {
	var val, exp, act string
	var jsn interface{}
	var w *bytes.Buffer
	var err error

	// https://ru.wikipedia.org/wiki/JSON
	val = `{
  firstName: Иван,
  lastName: Иванов,
  address: {
    streetAddress: "Московское ш., 101, кв.101",
    city: Ленинград,
    postalCode: 101101
  },
  phoneNumbers: [
    812 123-1234,
    916 123-4567
  ]
}
`
	jsn, err = ParseJson(strings.NewReader(val))
	if err != nil {
		panic(err)
	}
	w = new(bytes.Buffer)
	err = PrintJson(jsn, bufio.NewWriter(w))
	if err != nil {
		panic(err)
	}
	exp = val
	act = w.String()
	equal(t, exp, act, val)

	// https://en.wikipedia.org/wiki/JSON
	val = `{
  firstName: John,
  lastName: Smith,
  isAlive: true,
  age: 27,
  address: {
    streetAddress: 21 2nd Street,
    city: New York,
    state: NY,
    postalCode: 10021-3100
  },
  phoneNumbers: [
    {
      type: home,
      number: 212 555-1234
    },
    {
      type: office,
      number: 646 555-4567
    },
    {
      type: mobile,
      number: 123 456-7890
    }
  ],
  children: [],
  spouse: null
}
`
	jsn, err = ParseJson(strings.NewReader(val))
	if err != nil {
		panic(err)
	}
	w = new(bytes.Buffer)
	err = PrintJson(jsn, bufio.NewWriter(w))
	if err != nil {
		panic(err)
	}
	exp = val
	act = w.String()
	equal(t, exp, act, val)

}

func equal(t *testing.T, exp interface{}, act interface{}, val interface{}) {
	if exp != act {
		t.Errorf("'%v' <> '%v'; '%v'\n", exp, act, val)
	}
}
